package com.atguigu.flinksql.day10;

import com.atguigu.datastream.bean.WaterSensor;
import org.apache.flink.streaming.api.datastream.DataStream;
import org.apache.flink.streaming.api.datastream.DataStreamSource;
import org.apache.flink.streaming.api.datastream.SingleOutputStreamOperator;
import org.apache.flink.streaming.api.environment.StreamExecutionEnvironment;
import org.apache.flink.table.api.Table;
import org.apache.flink.table.api.bridge.java.StreamTableEnvironment;
import org.apache.flink.types.Row;

import static org.apache.flink.table.api.Expressions.$;

/**
 * ClassName: Test03_StreamToTable_PT
 * Package: com.atguigu.flinksql.day10
 * Description:
 *               添加处理时间
 * @Author ChenJun
 * @Create 2023/4/18 11:39
 * @Version 1.0
 */
public class Test03_StreamToTable_PT {
    public static void main(String[] args) throws Exception {

        //1. 创建运行环境
        StreamExecutionEnvironment env = StreamExecutionEnvironment.getExecutionEnvironment();
        env.setParallelism(1);
        StreamTableEnvironment tableEnv = StreamTableEnvironment.create(env);

        //2. 从端口读取数据
        DataStreamSource<String> streamSource = env.socketTextStream("localhost", 9999);
        SingleOutputStreamOperator<WaterSensor> waterSensorDS = streamSource.map(line -> {
            String[] split = line.split(",");
            return new WaterSensor(split[0], Long.parseLong(split[1]), Integer.parseInt(split[2]));
        });

        //TODO 3. 将流转换为动态表  同时添加处理时间
        Table table = tableEnv.fromDataStream(waterSensorDS, $("id"), $("ts"), $("vc"), $("pt").proctime());

        //TODO 4. 直接打印
        table.execute().print();


//        DataStream<Row> rowDataStream = tableEnv.toDataStream(table);
//        rowDataStream.print();
//
//        env.execute();

    }
}
